<?php
/**
 * 报到控制器
 */
namespace Home\Controller;
class RegistrationController extends BaseController {
    public function report(){
        $this->display();
    }
    //统计查询
    public function totalNumberReport(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $type = I('post.type','intdiv');
        $where['registrationDate'] = array('exp', 'IS NOT NULL');
        $where['enrollmentyear'] = date("Y");
        if($type==1){
            $result = M('student')->field(array('departmentvalue'=>'name','count(*)'=>'value'))->where($where)->group('name')->select();
        }else{
            //由于专业数较多只统计排名前15的数据
            $result = M('student')->field(array('majorvalue'=>'name','count(*)'=>'value'))->where($where)->group('name')->order('value desc')->limit(0,15)->select();
        }
        $this->ajaxReturn($result,"JSON");
    }

    public function registration(){
        $this->display();
    }
    public function doRegistration(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $data = array(
            'studentname' => I('post.studentName'),
            'sexcode' => I('post.sex'),
            'sexvalue' => I('post.sexValue'),
            'birthday' => strtotime(I('post.birthday')),
            'idcard' => I('post.idcard'),
            'examineenumber' => I('post.examineeNumber'),
            'nationcode' => I('post.nation'),
            'nationvalue' => I('post.nationValue'),
            'face' => I('post.studentFace'),
            'departmentcode' => I('post.department'),
            'departmentvalue' => I('post.departmentValue'),
            'majorcode' => I('post.major'),
            'majorvalue' => I('post.majorValue'),
            'postcode' => I('post.postcode'),
            'finishschool' => I('post.finishSchool'),
            'telphone' => I('post.telphone'),
            'parenttel' => I('post.parentTel'),
            'homeaddress' => I('post.homeAddress'),
            'registrationdate' => $_SERVER['REQUEST_TIME']
        );
        $where = array('id' => I('post.studentId'));
        if (M('student')->where($where)->save($data)) {
            $this->ajaxReturn(array("errorCode"=>0,"msg"=>"操作成功"),"JSON");
        }else{
            $this->ajaxReturn(array("errorCode"=>1,"msg"=>"操作失败"),"JSON");
        }
    }
    public function regisList(){
        $this->display();
    }

    public function getStudentByNoticeNumber(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $noticeNumber = I('post.noticeNumber');
        $where = array("noticenumber" => $noticeNumber);
        $where['enrollmentyear'] = date("Y");
        $student = M('student')->where($where)->select();
        $this->ajaxReturn($student,'JSON');
    }
    //分页查询已经报到的学生
    public function getStudentListByPage(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $pageIndex = I('post.page');
        $pageSize = I('post.rows');
        $studentName = I('post.q_studentName');
        $departmentCode = I('post.q_department','intdiv');
        $majorCode = I('post.q_major','intdiv');
        $dormId = I('post.q_dorm','intdiv');
        $where['registrationDate'] = array('exp', 'IS NOT NULL');
        $where['enrollmentyear'] = date("Y");
        if ($studentName!=''){
            $where['studentname'] = array('like','%'.$studentName.'%');
        }
        if($departmentCode != -1){
            $where['departmentcode'] = $departmentCode;
        }
        if($majorCode != -1){
            $where['majorcode'] = $majorCode;
        }
        if($dormId != -1){
            $where['dormcode'] = $dormId;
        }
        $count = M('student')->where($where)->count();
        $startno = ($pageIndex - 1) * $pageSize;
        $limit = $startno .',' . $pageSize;
        $studentList = M('student')->where($where)->order('registrationDate desc')->limit($limit)->select();
        $result = array(
            "total" => $count,   //记录总数
            "rows" => $studentList  //记录
        );
        $this->ajaxReturn($result,'JSON');
    }
    public function getCurrentDormInfo(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $dormcode = I('post.dormcode','intdiv');
        $where['dormcode'] = $dormcode;
        $result = M('student')->field('studentname,dormcode,dormposition')->where($where)->select();
        $this->ajaxReturn($result,'JSON');
    }
    public function doDisDorm(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $id = I('post.p_id','intdiv');
        $dormcode=I('post.p_dormcode','intdiv');
        $dormvalue=I('post.p_dormvalue');
        $dormposition=I('post.P_dormposition');
        if($dormcode == -1){
            $data = array(
                'dormcode' => null,
                'dormvalue' => null,
                'dormposition' => null
            );
        }else{
            $data = array(
                'dormcode' => $dormcode,
                'dormvalue' => $dormvalue,
                'dormposition' => $dormposition
            );
        }
        $where = array('id' => $id);
        if (M('student')->where($where)->save($data)) {
            $this->ajaxReturn(array("errorCode"=>0,"msg"=>"操作成功"),"JSON");
        }else{
            $this->ajaxReturn(array("errorCode"=>1,"msg"=>"操作失败"),"JSON");
        }
    }
}